package org.daidai.abstrusewriting.homepage.classify.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.classify.model.Special;

import java.util.Collection;
import java.util.List;


/**
 * Created by Administrator on 2016/10/15.
 */
public class SpecialAdapter extends BaseAdapter{
    private Context context;
    private List<Special> mList;
    private LayoutInflater mInflater;

    public SpecialAdapter(List<Special> list, Context context) {
        this.mList = list;
        this.context = context;
        mInflater=LayoutInflater.from(context);

        //L.d("进入了NewsAdapter mNewsList:",mList.toString());

    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null) {
            convertView=mInflater.inflate(R.layout.special_item,parent,false);
            convertView.setTag(new ViewHolder(convertView));
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.bindView(position,holder);

        return convertView;
    }


    public void addAll(Collection<? extends Special> collection){
        mList.addAll(collection);
        notifyDataSetChanged();
    }

    private  class ViewHolder{
        private ImageView image;
        private TextView title;
        private TextView dec;
        private TextView playCount;
        private TextView danmuCount;

        public ViewHolder(View itemView) {
            title=((TextView)itemView.findViewById(R.id.item_title));
            dec=((TextView)itemView.findViewById(R.id.item_dec));

            playCount=((TextView)itemView.findViewById(R.id.item_playcount));
            danmuCount=((TextView)itemView.findViewById(R.id.item_tracks));
            image=((ImageView)itemView.findViewById(R.id.item_imge));

        }

        private  void bindView(int position,ViewHolder view){
            if (mList != null) {
                if (mList.get(position) != null) {
                    Special special = mList.get(position);
                    title.setText(special.getTitle());
                     dec.setText(special.getIntro());
                    playCount.setText(String.valueOf(special.getPlaysCounts()));
                    danmuCount.setText(String.valueOf(special.getTracks()));
                    //ImageUtils.loadImage(image,Special.getPic());

                    Picasso.with(context)
                            .load(special.getCoverMiddle())
                            .config(Bitmap.Config.ARGB_8888)
                            .resize(100,100)
                            .into(image);

                  }
            }

        }
    }

}
